<template>
  <div class="container">
    <div class="box">
      <div class="signUp" v-if="!flag">
        <div class="title">
          <li style="font-size: 20px;color: black">星域VR</li>
          <li style="font-size: 12px;color:#909399 ">客服qq：24342106</li>
        </div>
        <input type="text" placeholder="邮箱" v-model="account">
        <input type="password" placeholder="密码" v-model="pwd">
        <div class="code">
          <input type="text" v-model="c" placeholder="验证码">
          <button @click="code">{{ btnText }}</button>
        </div>
        <input type="password" v-model="pwd" placeholder="新密码">

        <button @click="handelSign">验证</button>
      </div>

      <div class="login" v-if="!forget">
        <div class="title">
          <li style="font-size: 20px;color: black">星域VR</li>
          <li style="font-size: 12px;color:#909399 ">客服qq：24342106</li>
        </div>
        <input type="text" placeholder="邮箱" v-model="account">
        <input type="password" placeholder="密码" v-model="pwd">
        <button @click="login">
          <van-icon name="share-o"/>
          登陆
        </button>
        <div class="btm">
          <div><span @click="signUp">注册 </span> <span @click="Forget">忘记密码</span></div>
          <span> <van-icon name="list-switching"/> 简体中文</span>
        </div>
      </div>

      <div class="forget" v-if="forget">
        <div class="title">
          <li style="font-size: 20px;color: black">星域VR</li>
          <li style="font-size: 12px;color:#909399 ">客服qq：24342106</li>
        </div>

        <input type="text" placeholder="邮箱" v-model="account">
        <div class="code">
          <input type="text" v-model="c" placeholder="验证码">
          <button @click="code">{{ btnText }}</button>
        </div>
        <input type="password" placeholder="密码" v-model="pwd">

        <button @click="handelForget">验证</button>
      </div>
    </div>
    <van-floating-bubble axis="xy" icon="revoke" @click="onClick"/>

  </div>
</template>

<script setup>
import {ref, inject} from "vue";
import {showNotify} from "vant";
import {useRouter} from "vue-router";
const regEmail = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/
const btnText = ref("发送")
const flag = ref(true)
const forget = ref(false)
const account = ref("")
const pwd = ref("")
const c = ref("")
const btn = ref(1)
const axios = inject('axios')
const router = useRouter()
const login = () => {
  if (account.value.length > 3 && pwd.value.length > 4) {
    axios.post('login', {
      qq: account.value,
      pwd: pwd.value
    }).then(res=>{
      if (res.data.code == 1) {
        //showNotify({type: 'success', message: "登陆成功"});

        localStorage.setItem("qq", account.value)
        localStorage.setItem("ped", pwd.value)
        router.replace("/index")
      } else {
        showNotify({type: 'danger', message: res.data.msg});
      }

    }).catch(err=>{
      showNotify({type: 'danger', message: "w网络错误"});
    })
  } else {
    showNotify({type: 'danger', message: "请输入正确数据"});
  }

}
const onClick = () => {
  if (flag.value && !forget.value) {
    router.replace("/index")
  } else
    window.location.reload()
}
const Forget = () => {
  account.value = ''
  pwd.value = ""
  forget.value = true
}

const handelSign = () => {
  if (account.value.length > 3 && pwd.value.length > 4 && c.value.length == 6) {
    axios.post('login/sign', {
      qq: account.value,
      pwd: pwd.value,
      code: c.value
    }, 1, account.value)
  } else {
    showNotify({type: 'danger', message: "请输入正确数据"});
  }
}
const signUp = () => {
  account.value = ''
  pwd.value = ""
  flag.value = false

}
const handelForget = () => {
  if (account.value.length > 3 && pwd.value.length > 4 && c.value.length == 6) {
    axios.post('login/forget', {
      qq: account.value,
      pwd: pwd.value,
      code: c.value
    }, 1, account.value)
  } else {
    showNotify({type: 'danger', message: "请输入正确数据"});
  }
}
const code = () => {
  if (btn.value == 1) {
    if (!regEmail.test(account.value)) {
      showNotify({type: 'danger', message: "请输入正确的邮箱"});
    } else {
      btnText.value = "已发"
      btn.value = 0
      axios.get("login", {qq: account.value})
      setTimeout(() => {
        btn.value = 1
      }, 4000)
    }
  }
}
</script>

<style scoped lang="less">

.container {
  border: 0 solid transparent;
  background-color: #e8eaf1;
  height: 100vh;
  width: 100vw;
  box-sizing: border-box;
}

button {
  font-size: 14px;
}

.box {
  width: 80vw;
  height: 90vw;
  max-width: 400px;
  max-height: 300px;
  overflow: hidden;
  background-color: #ffffff;
  margin: 100px auto;
  border-radius: 10px;

  li {
    list-style: none;
    font-weight: 700;
  }

  .signUp,
  .login,
  .forget {
    display: flex;
    width: 100%;
    box-sizing: border-box;
    flex-direction: column;
  }

  input {
    width: 80%;
    height: 30px;
    background-color: #ebf2ff;
    margin: 10px auto;
    border: none;
    text-indent: 1em;
    font-size: 12px;
  }

  .title {
    margin: 10px auto
  }

  button {
    width: 80%;
    margin: 20px auto;
    background-color: #409EFF;
    border: none;
    border-radius: 3px;
    color: #ffffff;
    height: 30px;
  }

  .btm {
    display: flex;
    justify-content: space-between;
    width: 80%;
    margin: 10px auto;
    font-size: 12px;
    color: #909399;
  }

  .code {
    width: 80%;
    margin: 10px auto;
    display: flex;
    flex-direction: row;
    justify-content: space-between;

    input {
      width: 67%;
    }

    button {
      width: 30%;
      height: 30px;
      margin: 10px;
    }
  }
}


</style>